/*
 * @lc app=leetcode.cn id=1009 lang=javascript
 *
 * [1009] 十进制整数的反码
 */

// @lc code=start
/**
 * @param {number} n
 * @return {number}
 */
var bitwiseComplement = function (n) {
  if (n === 0) return 1;
  let result = 0;
  let stack = [];
  while (n) {
    stack.push(+!(n % 2));
    n = n / 2 >> 0;
  }
  while (stack.length > 0) {
    result = result * 2 + stack.pop();
  }
  return result;
};
// @lc code=end

